import Vue from 'vue'
import axios from 'axios'


// 响应时间
axios.defaults.timeout = 5 * 1000
// 配置cookie
// axios.defaults.withCredentials = true
// 配置请求头
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'

// 静态资源
Vue.prototype.$static = ''
// 配置接口地址
axios.defaults.baseURL = 'http://localhost:8080'
var loadingInstance
// POST传参序列化(添加请求拦截器)
axios.interceptors.request.use(function (config) {
	config.transformRequest = [function (data) {
		let ret = '';
		for (let it in data) {
			ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
		}
		return ret;
	}];
	return config;
},
function (error) {
	return Promise.reject(error);
})

// 发送请求
/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post (url, params) {
	return new Promise((resolve, reject) => {
		axios.post(url, params)
		.then(
			res => {
				resolve(res.data)
			},
			err => {
				reject(err.data)
			}
		)
		.catch(err => {
			reject(err.data)
		})
	})
}
/** 
 * get方法，对应get请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function get (url, params) {
	return new Promise((resolve, reject) => {
		axios.get(url, {
			params: params
		})
		.then(res => {
			resolve(res.data)
		})
		.catch(err => {
			reject(err.data)
		})
	})
}